package structure.stack;

import com.sun.org.apache.regexp.internal.RE;

import java.util.LinkedList;
import java.util.Stack;

public class geMaxWindows {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        int[] ans = process(arr,3);
        for (int a:ans) {
            System.out.println(a);
        }

    }
    public static int[] process(int[] arr,int max){
        if (arr == null || max <1 || arr.length <max){
            return null;
        }
        LinkedList<Integer> qmax = new LinkedList<>();
        int[] ans = new int[arr.length - max + 1];
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            while (!qmax.isEmpty() && arr[i] >= qmax.peek()){
                qmax.pollLast();
            }
               qmax.addLast(i);
            if (qmax.peekFirst() == i - max){
                qmax.pollFirst();
            }
            if (i>=max -1){
                ans[index++] = arr[qmax.peekFirst()];
            }
        }
        return ans;
    }

}
